package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.dto.result.ApiResponse;
import com.example.domain.entity.Device;
import com.example.domain.entity.Provider;
import com.example.domain.entity.ProviderComplaint;
import com.example.domain.query.DeviceQuery;
import com.example.domain.query.ProviderQuery;
import com.example.service.IProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private IProviderService providerService;

    @GetMapping("/list")
    public List<Provider> list() {
        return providerService.list();
    }

    @PostMapping("/page")
    public IPage<Provider> pageQuery(@RequestBody ProviderQuery query,
                                   @RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        return providerService.pageQuery(query, pageNum, pageSize);
    }

    @PostMapping("/save")
    public boolean save(@RequestBody Provider provider) {
        // 新增供应商时，默认审核状态为未审核
        provider.setAuditStatus(0);
        return providerService.save(provider);
    }

    @PostMapping("/update")
    public boolean update(@RequestBody Provider provider) {
        return providerService.updateById(provider);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable int id) {
        if (!providerService.removeById(id)) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "删除失败，供应商不存在");
        }
    }

    /**
     * 审核供应商
     */
    @PostMapping("/audit")
    public boolean audit(@RequestParam Integer providerId,
                         @RequestParam Integer auditStatus,
                         @RequestParam String auditComment,
                         @RequestParam Integer auditorId) {
        return providerService.audit(providerId, auditStatus, auditComment, auditorId);
    }

    /**
     * 提交供应商投诉
     */
    @PostMapping("/complaint")
    public boolean submitComplaint(@RequestBody ProviderComplaint complaint) {
        return providerService.submitComplaint(complaint);
    }

    /**
     * 更新供应商评级
     */
    @PostMapping("/updateRating")
    public boolean updateRating(@RequestBody Map<String, Object> params) {
        Integer providerId = (Integer) params.get("providerId");
        String rating = (String) params.get("rating");
        String reason = (String) params.get("reason");
        Integer operatorId = (Integer) params.get("operatorId");

        return providerService.updateRating(providerId, rating, reason, operatorId);
    }

    /**
     * 启用/禁用供应商
     */
    @PostMapping("/toggleActive")
    public boolean toggleActive(@RequestBody Map<String, Object> params) {
        Integer providerId = (Integer) params.get("providerId");
        Boolean isActive = (Boolean) params.get("isActive");

        return providerService.toggleActive(providerId, isActive);
    }
}
